package models

import (
	"time"
	"user_srv/basic/config"
)

// 用户发票表
type Invoice struct {
	Id                      uint64    `gorm:"column:id;type:bigint UNSIGNED;primaryKey;not null;" json:"id"`
	Name                    string    `gorm:"column:name;type:varchar(255);not null;" json:"name"`
	TaxIdentificationNumber string    `gorm:"column:tax_identification_number;type:varchar(255);comment:税号;not null;" json:"tax_identification_number"` // 税号
	Address                 string    `gorm:"column:address;type:varchar(255);not null;" json:"address"`
	Mobile                  string    `gorm:"column:mobile;type:varchar(255);not null;" json:"mobile"`
	BankOfDeposit           string    `gorm:"column:bank_of_deposit;type:varchar(255);comment:开户银行;not null;" json:"bank_of_deposit"` // 开户银行
	BankAccount             string    `gorm:"column:bank_account;type:varchar(255);comment:银行账户;not null;" json:"bank_account"`       // 银行账户
	CreatedAt               time.Time `gorm:"column:created_at;type:datetime(3);default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt               time.Time `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt               time.Time `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (b *Invoice) TableName() string {
	return "Invoice"
}

// 创建发票
func (b *Invoice) Create() error {
	return config.DB.Create(b).Error
}
func (b *Invoice) GetInvoiceById() error {
	return config.DB.Where("id = ?", b.Id).Find(&b).Error
}

func (b *Invoice) GetTaxIdentificationNumber() (list []*Invoice, err error) {
	err = config.DB.Where("tax_identification_number = ?", b.TaxIdentificationNumber).Find(&list).Error
	return
}

// 发票详情
func (b *Invoice) Detail() error {
	return config.DB.Where("id = ?", b.Id).Find(&b).Error
}
